/*
* Project : Linuxbox
* File    : AdminLogin.java
* Version : 1.0
* Author  : Kevin Zhang
*
* Copyright (c) 2000-2001 by Linuxbox, Inc.
* All rights reserved.
 */
package linuxbox.system.servlet;

import javax.servlet.http.*;

import java.io.*;

import linuxbox.BoxStatic;
import linuxbox.common.*;

public class ShutdownAction extends Action {
  protected static String successJSP = null;

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse res) throws Exception {
    if (aBoxSession.isSystemAdmin()) {
      Runtime rt = Runtime.getRuntime();
      rt.exec("/etc/rc.d/init.d/postgresql stop");
      Process p = rt.exec("halt");
      InputStream is = p.getInputStream();
      byte[] buff = new byte[1024];
      is.read(buff);
      System.out.println(new String(buff));
      is.close();

      is = p.getErrorStream();
      buff = new byte[1024];
      is.read(buff);
      System.out.println(new String(buff));
      is.close();
      res.sendRedirect(successJSP);
    }
    else{
      redirectFile(BoxStatic.accessRightErrorJSP, req, res);
    }
  }

  public void setSuccessJSP(String str) {
    successJSP = str;
  }
}